﻿Imports Microsoft.WindowsCE.Forms


Public Class frmPrj

    Private Sub frmPrj_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
        MyShared.myDb.CloseConn()
    End Sub

    Private Sub frmPrj_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        '调试前设置注册码
        If GCS_Tools.DeviceID.WriteDiviceID("水准测量") Then
        End If

        '正式版执行
        If GCS_Tools.DeviceID.ExamDeviceID("水准测量") Then

            LoadPrepare()
        Else
            MsgBox("未经注册，恕不运行！")
            Application.Exit()
            Me.Close()
        End If

    End Sub
    Private Sub LoadPrepare()
        Dim mshared As New MyShared
        UpdateListWithDb()

        SelectCurrent()
        SearchIexam()
        InputPanel1.Enabled = False

    End Sub


    Private Sub frmPrj_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
        SelectCurrent()
    End Sub
    Private Sub UpdateListWithDb()
        '载入已有项目编号
        lstPrjs.Items.Clear()
        lstPrjs.Items.Add("新建项目")
        Dim sV() As String = MyShared.myDb.ProjectRead.Split(vbCrLf)

        For Each s As String In sV
            If s.Trim.Length > 0 Then
                lstPrjs.Items.Add(s.Trim)
                bSet.Enabled = True
                bDel.Enabled = True
            End If

        Next
    End Sub
    Private Sub SelectCurrent()
        'Dim mDb As New DbLevel
        Dim mCurrent As New LevelDatabase.CurrentOption
        mCurrent = MyShared.myDb.CurrentRead

        For I As Integer = 0 To lstPrjs.Items.Count - 1
            If lstPrjs.Items(I) = mCurrent.CurrentProject Then
                lstPrjs.SelectedIndex = I
                bIsv.Enabled = True
                bRead.Enabled = True
                Exit For
            End If
        Next
    End Sub
    Private Sub SearchIexam()
        'Dim db As New DbLevel
        Dim mExam As New clsExamI
        MyShared.myDb.IExamReadLast(mExam)
        If mExam.Opts.sDate = String.Empty Then
            lblNote.Text = "没有i角检测记录"
        Else
            Dim days As Integer = (Now.Date - CDate(mExam.Opts.sDate)).TotalDays
            lblNote.Text = "最近I角检测日期：" & mExam.Opts.sDate

            If days > 20 Then
                lblNote.BackColor = Color.Red
                lblNote.Text &= vbCrLf & ("已严重拖期！应马上进行I角检测！")
            ElseIf days > 15 Then
                lblNote.BackColor = Color.Red
                lblNote.Text &= vbCrLf & ("已拖期！应马上进行I角检测！")
            ElseIf days > 14 Then
                lblNote.BackColor = Color.Red
                lblNote.Text &= vbCrLf & ("满15天，应马上进行I角检测！")
            ElseIf days > 12 Then
                lblNote.BackColor = Color.Pink
                lblNote.Text &= vbCrLf & ("距离下次检测还有" & 15 - days & "天")
            Else
                lblNote.Text &= vbCrLf & ("距离下次检测还有" & 15 - days & "天")
            End If
        End If

    End Sub

#Region "新建项目"
    Private Sub bNewPrjOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bNewPrjOk.Click
        If txtYear.Text.Length > 0 And txtType.Text.Length > 0 And txtNumber.Text.Length > 0 Then
            Dim newItm As String = txtYear.Text & "-" & txtType.Text & "-" & txtNumber.Text
            Dim Exist As Boolean = False
            For Each itm As String In lstPrjs.Items
                If itm = newItm Then
                    Exist = True
                    Exit For
                End If
            Next
            If Not Exist Then
                'Dim mDb As New DbLevel
                MyShared.myDb.AddProjectRow(newItm)
                UpdateListWithDb()
            End If

        End If
        pnlNewPrj.Visible = False
    End Sub

    Private Sub txtYear_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtYear.GotFocus
        ListBox1.Visible = True
        ListBox1.Items.Clear()
        ListBox1.Left = txtYear.Left
        ListBox1.Top = txtYear.Bottom
        ListBox1.Width = txtYear.Width
        Dim Year As Integer = Now.Year
        ListBox1.Items.Add(Year.ToString)
        ListBox1.Items.Add((Year - 1).ToString)
        ListBox1.Items.Add((Year - 2).ToString)
    End Sub

    Private Sub txtType_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtType.GotFocus
        ListBox1.Visible = True
        ListBox1.Items.Clear()
        ListBox1.Left = txtType.Left
        ListBox1.Top = txtType.Bottom
        ListBox1.Width = txtType.Width
        ListBox1.Items.Add("CB")
        ListBox1.Items.Add("CS")
        ListBox1.Items.Add("CH")
    End Sub

    Private Sub txtNumber_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtNumber.GotFocus
        ListBox1.Visible = False
        InputPanelSet.SetNumeric(InputPanel1)
        InputPanel1.Enabled = True
    End Sub
    Private Sub txtNumber_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtNumber.LostFocus
        InputPanel1.Enabled = False
    End Sub

    Private Sub ListPrjs_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstPrjs.SelectedIndexChanged
        If lstPrjs.SelectedItem = "新建项目" Then
            pnlNewPrj.Visible = True

        Else
            'Dim mDb As New DbLevel
            Dim pc As New LevelDatabase.CurrentOption
            MyShared.myDb.CurrentRecordRow("CurrentProject", lstPrjs.SelectedItem)

        End If
    End Sub

    Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
        If Math.Abs(ListBox1.Left - txtYear.Left) < 5 Then
            txtYear.Text = ListBox1.SelectedItem
            txtType.Focus()
        ElseIf Math.Abs(ListBox1.Left - txtType.Left) < 5 Then
            txtType.Text = ListBox1.SelectedItem
            txtNumber.Focus()
            ListBox1.Visible = False
        End If

    End Sub

#End Region

    ''' <summary>
    ''' 删除项目
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bDel.Click
        Dim iPos As Integer = lstPrjs.SelectedIndex
        If iPos > 0 Then
            If MsgBox("真要删除项目编号" & lstPrjs.SelectedItem & "吗？" & _
                         vbCrLf & "Yes(确认)/No(取消)", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
                'Dim mDb As New DbLevel
                MyShared.myDb.ProjectDeleteRow(lstPrjs.Items(iPos))
                UpdateListWithDb()

                lstPrjs.SelectedIndex = 0
            End If
        End If
    End Sub

    ''' <summary>
    ''' 检测I角
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bIsv.Click
        Dim frmWk As New frmTeam
        frmWk.ShowDialog()
        frmWk.Dispose()

        Dim fInstr As New frmInstr
        fInstr.ShowDialog()
        fInstr.Dispose()

        Dim frmExamI As New frmExamI
        frmExamI.ShowDialog()
        frmExamI.Dispose()

        SearchIexam()
    End Sub

    ''' <summary>
    ''' 设置项目参数
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bSet.Click
        Dim FrmOpt As New frmOption
        FrmOpt.ShowDialog()
        FrmOpt.Dispose()
    End Sub

    ''' <summary>
    ''' 设置观测参数
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bRead.Click
        Dim frmWk As New frmTeam
        frmWk.ShowDialog()
        frmWk.Dispose()

        Dim fInstr As New frmInstr
        fInstr.ShowDialog()
        fInstr.Dispose()

        Dim frmFL As New frmFile
        frmFL.ShowDialog()
        frmFL.Dispose()

        Dim frmRd As New frmRead
        frmRd.ShowDialog()
        frmRd.Dispose()
    End Sub

End Class

Public Class InputPanelSet
    Public Shared CurrenMod As String

    Shared Sub SetNumeric(ByRef InputPanel1 As InputPanel)
        If SetInputMod("Resco Keyboard", InputPanel1) Then
            CurrenMod = "Nm"
        End If
    End Sub

    Shared Sub SetChinese(ByRef InputPanel1 As InputPanel)
        If SetInputMod("全屏手写输入", InputPanel1) Then
            CurrenMod = "Ch"
        End If
    End Sub

    Shared Function SetInputMod(ByVal ModStr As String, ByRef InputPanel1 As InputPanel) As Boolean
        Try
            For Each vs As InputMethod In InputPanel1.InputMethods
                If vs.Name = ModStr Then
                    InputPanel1.CurrentInputMethod = vs
                    Return True
                End If
            Next
        Catch ex As Exception

        End Try
        Return False
    End Function
End Class